﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using Common;

namespace WebApplication1.testThread
{
    /// <summary>
    /// Handler 的摘要说明
    /// </summary>
    public class Handler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            byte[] bytes = new byte[1024];
            int length = context.Request.InputStream.Read(bytes, 0, bytes.Length);
            string condition = Encoding.UTF8.GetString(bytes);

            int id = int.Parse(condition.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries)[1]);
            //根据id查找Person对象
            var person = GetPersonList().Where(x => x.ID == id).First();

            //将Person格式化为二进制数据写入outPutStream
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(context.Response.OutputStream, person);
        }

        private IList<Person> GetPersonList()
        {
            var personList = new List<Person>();
            var person1 = new Person();
            person1.ID = 1;
            person1.age = 29;
            person1.name = "李世杰";
            personList.Add(person1);
            return personList;
        }

        public bool IsReusable
        { get { return true; } }
    }
}